<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .red {background-color:red; }
    </style>
</head>
<body>
    <ul>
        <li class="red">1aaaa</li>
        <li>2aaaa</li>
        <li>3aaaa</li>
        <li>4aaaa</li>
        <li>5aaaa</li>
    </ul>
    <script type="text/javascript">
        var className = 'red';
        var lisEle = document.querySelectorAll('ul li');
        document.addEventListener('keydown',function(event){
            for(var i=0; i<lisEle.length; i++){
                if(lisEle[i].className == className){
                    if(event.keyCode == 38 || event.keyCode == 40 ){
                        lisEle[i].className = '';
                        lisEle[ (event.keyCode == 38) ? (i==0?lisEle.length-1: i-1) :  (event.keyCode == 40) ? (i == lisEle.length-1? 0 : i+1) : 0  ].className = className;
                        break;
                    }
                }
            }
        });
    </script>
</body>
</html>
